//
//  ST_DesignOpenWayTipsController.swift
//  btvideo
//
//  Created by Jason on 2022/3/4.
//  Copyright © 2022 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyAttributes
import WebKit

struct ST_DesignOpenWayStruct {
    let title:String
    let preUrl:URL
    let judge_u_r_l:URL
}

class ST_DesignOpenWayTipsController: UIViewController,ST_VMControllerLightNavBarProduce {
    
    private let previwe_u_r_l:URL
    private let judge_u_r_l:URL
    
    init(way:ST_DesignOpenWayStruct){
        
        self.judge_u_r_l = way.judge_u_r_l
        self.previwe_u_r_l = way.preUrl
        super.init(nibName: nil, bundle: nil)
        self.title = way.title
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        defer {
            layout_navigation_bar()
        }
        
        view.backgroundColor = .white
        webView.backgroundColor = .clear
        webView.isOpaque = false
        
        self.view.addSubview(webView)
        let line = UIView()
        line.backgroundColor = UIColor.init(hex: 0x999999, transparency: 0.3)
        self.view.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(statusBarHeight+44)
            make.left.right.equalTo(self.view)
            make.height.equalTo(1)
        }
        webView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self.view)
            make.top.equalTo(line.snp.bottom)
        }
        
        
        if let url = URL.init(string: PH.tipsStepHtml){
            webView.load(URLRequest(url: url))
        }
        
        
    }
    
    //MARK: - Properties
    
    lazy var tain_navigation_bar:ST_TIANLightNavigationBar = {
        let c = ST_TIANLightNavigationBar()
        c.backgroundColor = .clear
        c.title_nav_btn.setTitle(self.title, for: .normal)
        c.right_nav_btn.setImage(ST_ImageUtils.imageName("more_black"), for: .normal)
        return c
    }()
    
    private let webView = WKWebView()
    
    
}


extension ST_DesignOpenWayTipsController:ST_TIANLightNavigationBarAction{
    
    func navigation_item_bar_tap_subscribe(position:ST_TIANLightNavigationBarItemPosition){
        
        switch position {
        case .left:goback_previous_controller()
        case .right:
            GlobalPresent(toController: DesignWayShuttleController(url: judge_u_r_l))
        default:
            break
        }
    }
    
}



private class DesignWayShuttleController:UIViewController{
    
    private let judge_u_r_l:URL
    
    init(url:URL){
        judge_u_r_l = url
        super.init(nibName: nil, bundle: nil)
        self.overCurrentContext = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { make in
            make.height.equalTo(202+20+BTSafeAreaBottom)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(20)
        }
        
        root_view.addSubview(open_btn)
        open_btn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(21)
            make.top.equalToSuperview()
            make.width.equalTo(56)
            make.height.equalTo(115)
        }
        
        root_view.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(open_btn.snp.bottom)
            make.height.equalTo(1)
        }
        
        
        root_view.addSubview(cancel_btn)
        cancel_btn.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(open_btn.snp.bottom).offset(2)
            make.height.equalTo(44)
        }
    }
    
    fileprivate lazy var root_view:UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.cornerRadius = 20
        return v
    }()
    
    
    fileprivate lazy var open_btn:ST_TianButton = {
        let btn = ST_TianButton(style: .image_top_title_bottom(imageTop: 22, imageSize: CGSize(width: 40, height: 40), layout: ST_TianButtonLayoutModel.outside_stable(lastControlEdgeValue: -27)))
        btn.setImageUrl("http://btstatic-cdn.jingniaoapps.com/dgkr71m1646386629".url, for: .normal)
        btn.setTitle("浏览器打开".withAttributes([.font(.systemFont(ofSize: 12, weight: .regular)),.textColor(UIColor(hex: 0xACB4C0)!)]), for: .normal)
        btn.addTarget(self, action: #selector(click_handle), for: UIControl.Event.touchUpInside)
        return btn
    }()
    
    fileprivate lazy var cancel_btn:UIButton = {
        let btn = UIButton.init(type: UIButton.ButtonType.custom)
        btn.setAttributedTitle("取消".withAttributes([.font(.systemFont(ofSize: 15, weight: .regular)),.textColor(UIColor(hex: 0x333333)!)]), for: .normal)
        btn.backgroundColor = .clear
        btn.addTarget(self, action: #selector(cancel_handle), for: UIControl.Event.touchUpInside)
        return btn
    }()
    
    
    fileprivate lazy var line:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xE7E9ED)
        return v
    }()
    
    
    @objc fileprivate func cancel_handle(){
        
        self.goback_previous_controller()
    }
    
    @objc fileprivate func click_handle(){
        
        UIApplication.shared.open(judge_u_r_l)
    }
}






